package com.zby.simulationHibernate.util.factory;

import com.zby.simulationHibernate.util.common.CommonUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * Created By zby on 16:48 2019/1/23
 */

public class SessionClose {

    /**
     * Created By zby on 22:45 2019/1/23
     * <p>
     * 当关闭当前会话时，这并非真正的关闭会话
     * 只是将连接对象放回到连接池中
     */
    public static void closeConn(Connection connection) {
        SqlSessionFactory.getConnections().add(connection);
    }

    /**
     * Created By zby on 17:08 2019/1/23
     * 关闭预编译的SQL语句
     */
    public static void closeState(Statement statement) {
        if (CommonUtil.isNotNull(statement)) {
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Created By zby on 17:08 2019/1/23
     * 关闭数据集
     */
    public static void closeResultSet(ResultSet resultSet) {
        if (CommonUtil.isNotNull(resultSet)) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * Created By zby on 17:08 2019/1/23
     * 关闭连接和预编译语句
     */
    public static void closeConnAndStatement(Connection connection, Statement statement) {
        closeConn(connection);
        closeState(statement);
    }

    /**
     * Created By zby on 17:08 2019/1/23
     * 关闭连接、预编译语句、数据集
     */
    public static void closeConnStateResSet(Connection connection, Statement statement, ResultSet resultSet) {
        closeConn(connection);
        closeState(statement);
        closeResultSet(resultSet);
    }
}
